using System;
using System.Drawing;
using System.Xml;

namespace FreeFlow.Client
{
	/// <summary>
	/// A Label field.
	/// </summary>
	[Serializable]
	public class LabelField : Field
	{
    internal LabelField(Form form, XmlElement fieldNode)
      : base(form, fieldNode)
		{
      if (fieldNode != null)
      {
        caption = fieldNode.GetAttribute("Caption");	
        align = ParseAlign(fieldNode.GetAttribute("Align"));
        XmlElement fontNode = fieldNode["Font"];
        if (fontNode != null)
        {
          font = ParseFont(fontNode);
          fontColor = ParseFontColor(fontNode);
        }
        linkField = fieldNode.GetAttribute("LinkField");
      }

      // get the URL, if there is one
      if (ClientExtensions != null)
      {
        if (ClientExtensions.StartsWith("URL="))
        {
          try
          {
            url = new Uri(ClientExtensions.Substring(4));
          }
          catch(UriFormatException)
          {
            // ignore, couldn't recognise the URL
          }
        }
      }
		}

    internal override bool ShouldAddToFieldInput()
    {
      return false;
    }

    #region properties

    /// <summary>
    /// Gets the hint for the label (for a label showing a URL, the hint will be the URL)
    /// </summary>
    public override string Hint
    {
      get 
      { 
        if (url == null)
          return base.Hint;
        else
          return url.ToString();
      }
    }

    private string linkField;
    /// <summary>
    /// Gets the name of the field that this label is linked to.
    /// </summary>
    public string LinkField
    {
      get { return linkField; }
    }

    private Font font;
    /// <summary>
    /// Gets the font of the label.
    /// </summary>
    public Font Font
    {
      get { return font; }
    }

    private Color fontColor;
    /// <summary>
    /// Gets the color of the font of the label.
    /// </summary>
    public Color FontColor
    {
      get { return fontColor; }
    }

    private string caption;
    /// <summary>
    /// Gets the caption of the label.
    /// </summary>
    public string Caption
    {
      get { return caption; }
    }

    private Align align;
    /// <summary>
    /// Gets the alignment of the label.
    /// </summary>
    public Align Align
    {
      get { return align; }
    }

    private Uri url;
    /// <summary>
    /// Gets the URL if the label will display a hyperlink.
    /// </summary>
    public Uri Url
    {
      get
      {
        return url;
      }
    }

    #endregion
	}
}
